package pers.flowerinsnow.minecraft.forge.shiftability

import net.minecraft.client.Minecraft
import net.minecraft.util.ChatComponentText
import net.minecraftforge.client.event.ClientChatReceivedEvent
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.fml.common.Mod
import net.minecraftforge.fml.common.event.FMLInitializationEvent
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent
import pers.flowerinsnow.minecraft.forge.shiftability.listener.KeyListener
import pers.flowerinsnow.minecraft.forge.shiftability.listener.TickListener
import pers.flowerinsnow.minecraft.forge.shiftability.manager.ISAConfigManager
import pers.flowerinsnow.minecraft.forge.shiftability.manager.KeyManager
import pers.flowerinsnow.minecraft.forge.shiftability.manager.impl.SAConfigManagerImpl

@Mod(
    modid = ShiftAbility.MODID,
    version = ShiftAbility.VERSION,
    dependencies = "required-after:minecraftkotlin",
    clientSideOnly = true
)
class ShiftAbility {
    @Mod.EventHandler
    fun preInit(event: FMLPreInitializationEvent) {
        configManager = SAConfigManagerImpl(event.suggestedConfigurationFile);
    }

    @Mod.EventHandler
    fun init(event: FMLInitializationEvent) {
        KeyManager.registerKeys(KeyManager::class.java)
        registerListeners(KeyListener(), TickListener())
    }

    @Mod.EventHandler
    fun postInit(event: FMLPostInitializationEvent) {

    }
    private fun registerListeners(vararg listeners : Any) {
        for (listener in listeners) {
            MinecraftForge.EVENT_BUS.register(listener)
        }
    }

    companion object {
        const val MODID = "shiftability"
        const val VERSION = "0 Alpha 8"

        lateinit var configManager: ISAConfigManager
        fun addChat(chat : String) {
            val text = ChatComponentText(chat)
            val event = ClientChatReceivedEvent(0.toByte(), text)
            MinecraftForge.EVENT_BUS.post(event)
            if (!event.isCanceled)
                Minecraft.getMinecraft().thePlayer.addChatComponentMessage(event.message)
        }
    }
}
